function CodePanel(){
}

CodePanel.instance = CodePanel;

CodePanel.faceUrl = "codeController";

/**
 * 暂存数据
 */
CodePanel.data = [];

CodePanel.codeTreegrid = null;

CodePanel.codeEditWindow = null;

//初始
CodePanel.init = function(){
	CodePanel.codeTreegrid = mini.get("codeTreegrid");
	CodePanel.codeEditWindow = mini.get("codeEditWindow");
	CodePanel.reload();
};

/**
 * 加载表格数据
 */
CodePanel.reload = function(){
	
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/" + CodePanel.faceUrl,
		cache: false,
		data: {
			method: "find"
		},
		success: function(json) {
			if (json) {
				CodePanel.data = json;
				CodePanel.codeTreegrid.loadData(json);
			}
			return;
		},
		error: function(obj, ertxt) {// 失败回调函数.这里是网络失败, 比如500错误等
			mini.alert("系统错误：" + ertxt);
		}
	});
};

/**
 * 增加
 */
CodePanel.add = function(){
	
	var data = CodePanel.codeTreegrid.getSelectedNode();
	var fdata = {
		'pid': '0',
		'pName': '顶级节点'
	};

	if (data) {
		fdata.pid = data.id;
		fdata.pName = data.name;
	}
	
	var form = new mini.Form("#codeEditform");
	form.clear();
	form.setData(fdata);
	CodePanel.codeEditWindow.show();
};

//编辑
CodePanel.edit = function(){
	var data = CodePanel.codeTreegrid.getSelectedNode();
	if (data) {
		var obj = MiniuiTools.findTreeNode(data.pid, CodePanel.data);
		if (!obj){
			obj = {id: '0',name: '顶级节点',pid: '-1'};
		}
		data.pName = obj.name;
		var form = new mini.Form("#codeEditform");
		form.clear();
		form.setData(data);
		CodePanel.codeEditWindow.show();
	} else {
		mini.alert("请选中要编辑的编码！");
	}
};

//删除
CodePanel.del = function() {
	var data = CodePanel.codeTreegrid.getSelectedNode();

	if(!data){
		mini.alert("请选中要删除的记录！");
	}else{
		mini.confirm("确定要删除编码名称为  " + "<span style='color: red;'> " +data.name + "</span>" + "  的编码吗？", "删除编码",
	            function (action) {    
			  if (action == "ok") {
				  var ids = new Array();
				  ids.push(data.id);
					$.ajax( {
						type: 'POST',
						dataType: 'json',
						url: __root+"/"+CodePanel.faceUrl,
						data: {
							method: 'delete',
							id: ids,
							lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
						},
						success: function(text) {
							CodePanel.reload();
							return;
						},
						error: function(obj, error) {
							CodePanel.reload();
						}
					});
			  }
		});
	}
};

//关闭窗口
CodePanel.closeButton = function(){
	CodePanel.codeEditWindow.hide();
};

//保存
CodePanel.save = function() {
	
	var form = new mini.Form("#codeEditform");
	
	form.validate();
    if (form.isValid() == false) return;
	
	var data = form.getData();
	
	data.method = "save";
	data.lslUser = gbOperateUser;
	data.lslAddress = gbOperateIp;

	data.data	= data.data + "\n";
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+CodePanel.faceUrl,
		data: data,
		success: function(text) {
			CodePanel.reload();
			CodePanel.closeButton();
			return;
		}
	});
};